﻿using System;
using System.IO;
using System.Windows.Media;
using System.Xml;
using Office = Microsoft.Office.Core;
using PPT = Microsoft.Office.Interop.PowerPoint;

namespace An_Im.CoreManipulatorUtil
{
    public static class StringHelper
    {
        /// <summary>
        /// 自动将字符串 x 转换为前缀的注释符号。
        /// </summary>
        public static string ToPrefix(this string str)
        {
            if (str.Length == 0)
                return "\'";
            else if (str[0] == 'x')
                return "\'";
            else return string.Empty;
        }

        /// <summary>
        /// 自动将字符串 x 转换为前缀的注释符号。
        /// </summary>
        public static string ToPrefix(this object str)
        {
            if (str == null)
                return "\'";
            else return string.Empty;
        }

        /// <summary>
        /// 对非进入退出动画实现处理。
        /// </summary>
        public static string ToAppearPrefix(this string str)
        {
            PPT.MsoAnimEffect enumIndex = (PPT.MsoAnimEffect)Enum.Parse(typeof(PPT.MsoAnimEffect), str);
            if ((int)enumIndex == 52 || (int)enumIndex >= 54)
                return "\'";
            else
                return String.Empty;
        }

        /// <summary>
        /// 对非进入退出动画实现处理。
        /// </summary>
        public static string ToAppearPrefix(this PPT.MsoAnimEffect enumIndex)
        {
            if ((int)enumIndex == 52 || (int)enumIndex >= 54)
                return "\'";
            else
                return String.Empty;
        }

        /// <summary>
        /// 检查其是否可以设置平滑。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToSmoothPrefix(this string str)
        {
            if (str.Length == 0)
                return "\'";
            else if (str[0] == 'x')
                return "\'";
            else
            {
                Office.MsoTriState enumIndex = (Office.MsoTriState)Enum.Parse(typeof(Office.MsoTriState), str);
                if (enumIndex == Office.MsoTriState.msoFalse)
                    return "\'";
                else
                    return String.Empty;
            }

        }


        public static string ToSmoothPrefix(this object str)
        {
            if (str == null)
                return "\'";
            else
            {
                if (str is Office.MsoTriState)
                {
                    if ((Office.MsoTriState)str == Office.MsoTriState.msoFalse)
                        return "\'";
                    else
                        return String.Empty;
                }

            }
            return string.Empty;
        }

    }

}

namespace An_Im
{


    static partial class CoreManipulator
    {
        private static string GetTimeDetail(PPT.Timing tim)
        {
            return string.Format(Properties.Resources.TimeDetail,
               tim.Duration,
               tim.TriggerDelayTime,
               tim.RepeatCount == int.MinValue ? "forever" : tim.RepeatCount.ToString(),
               tim.SmoothStart,
               tim.SmoothEnd,
               tim.BounceEnd,
               tim.Accelerate,
               tim.Decelerate,
               tim.BounceEndIntensity,
               tim.AutoReverse);
        }


        /// <summary>
        /// 设置要素的时间。
        /// </summary>
        /// <param name="xmlele"></param>
        /// <param name="tim"></param>
        private static void SetTimeDetail(XmlElement xmlele, TimeInformation tim)
        {
            xmlele.AddXmlAttribute("时长", tim.Duration);
            xmlele.AddXmlAttribute("延迟", tim.Delay);
            xmlele.AddXmlAttribute("重复", tim.Repeat);
            xmlele.AddXmlAttribute("平滑", $"{tim.SmoothStart},{tim.SmoothEnd}");
            xmlele.AddXmlAttribute("弹跳", tim.BounceEnd);
            xmlele.AddXmlAttribute("加速", tim.Accelerate);
            xmlele.AddXmlAttribute("减速", tim.Decelerate);
            xmlele.AddXmlAttribute("弹跳速度", tim.BounceEndIntensity);
            xmlele.AddXmlAttribute("自动返回", tim.AutoReverse);
        }

        private static void AddXmlAttribute(this XmlElement xmlele, string attName, object attValue)
        {
            var doc = xmlele.OwnerDocument;
            var att = doc.CreateAttribute(attName);
            att.Value = attValue == null ? "" : attValue.ToString();
            xmlele.Attributes.Append(att);
        }

        private static void AddXmlAttributeX(this XmlElement xmlele, string attName, object attValue)
        {
            var doc = xmlele.OwnerDocument;
            var att = doc.CreateAttribute(attName);
            att.Value = attValue == null ? "x" : attValue.ToString();
            xmlele.Attributes.Append(att);
        }

        private static string GetTimeDetail(TimeInformation tim)
        {
            return string.Format(Properties.Resources.TimeDetail,
               tim.Duration,
               tim.Delay,
               tim.Repeat,
               tim.SmoothStart,
               tim.SmoothEnd,
               tim.BounceEnd,
               tim.Accelerate,
               tim.Decelerate,
               tim.BounceEndIntensity,
               tim.AutoReverse);
        }

        /// <summary>
        /// 设置动画细节。
        /// </summary>
        /// <param name="timeInfo"></param>
        /// <param name="tim"></param>
        private static void SetTimeDetail(TimeInformation timeInfo, PPT.Timing tim)
        {
            timeInfo.Duration = tim.Duration;
            timeInfo.Delay = tim.TriggerDelayTime;
            timeInfo.Repeat = tim.RepeatCount == int.MinValue ? 999 : tim.RepeatCount;
            timeInfo.SmoothStart = tim.SmoothStart;
            timeInfo.SmoothEnd = tim.SmoothEnd;
            timeInfo.BounceEnd = tim.BounceEnd;
            timeInfo.Accelerate = tim.Accelerate;
            timeInfo.Decelerate = tim.Decelerate;
            timeInfo.BounceEndIntensity = tim.BounceEndIntensity;
            timeInfo.AutoReverse = tim.AutoReverse;
        }

        /// <summary>
        /// 字符转二进制
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private static int CharToHex(char ch)
        {
            if ((int)ch >= (int)'0' && (int)ch <= (int)'9')
                return (int)ch - (int)'0';
            else if ((int)ch >= (int)'a' && (int)ch <= (int)'f')
                return (int)ch - (int)'a' + 10;
            else if ((int)ch >= (int)'A' && (int)ch <= (int)'F')
                return (int)ch - (int)'A' + 10;
            else
                return (int)ch;
        }

        /// <summary>
        /// 十六进制转为颜色对象。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static Color StringToColor(string str)
        {
            var red = (byte)(CharToHex(str[1]) * 16 + CharToHex(str[2]));
            var green = (byte)(CharToHex(str[3]) * 16 + CharToHex(str[4]));
            var blue = (byte)(CharToHex(str[5]) * 16 + CharToHex(str[6]));

            return Color.FromRgb(red, green, blue);
        }

        /// <summary>
        /// 获得图片源。
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static ImageSource GetImageSource(string fileName)
        {
            var svgbytes = Properties.Resources.ResourceManager.GetObject(fileName) as byte[];
            using (var stream = new MemoryStream(svgbytes, false))
            {
                return Svg2Xaml.SvgReader.Load(stream);
            }
            return null;
        }

        private static PPT.MsoAnimEffect StringToPPTAnimType(string str)
        {
            PPT.MsoAnimEffect enumIndex = (PPT.MsoAnimEffect)Enum.Parse(typeof(PPT.MsoAnimEffect), str);
            return enumIndex;
        }

        private static string GetValidVBASubName(string name)
        {
            if (name.StartsWith("PA-"))
                name = name.Substring(3);
            name = name.Replace('-', '_');
            return name.Replace(' ', '_').TrimStart('_');
        }

        /// <summary>
        /// 获得重复动画的数据。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string GetRepeat(string value)
        {
            if (value == "forever") return "999";
            else return value;
        }

        /// <summary>
        /// 仅检查数据可行性。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool GetValueAvaliable(string str)
        {
            if (str.Length == 0)
                return false;
            else if (str[0] == 'x')
                return false;
            else return true;
        }


        /// <summary>
        /// 获得平滑的可行性。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool GetSmoothAvaliable(string str)
        {
            if (str.Length == 0)
                return false;
            else if (str[0] == 'x')
                return false;
            else
            {
                Office.MsoTriState enumIndex = (Office.MsoTriState)Enum.Parse(typeof(Office.MsoTriState), str);
                if (enumIndex == Office.MsoTriState.msoFalse)
                    return false;
                else
                    return true;
            }

        }


    }
}
